package com.cloud.order.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.math.BigDecimal;
import java.util.ArrayList;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.cloud.order.entity.Order;
import com.cloud.order.feign.ProductFeignClient;
import com.cloud.order.service.OrderService;
import com.cloud.product.entity.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * packageName com.cloud.order.service.impl
 *
 * @author yanshuanglong
 * @version JDK 8
 * @className OrderServiceImpl
 * @date 2025/3/17
 * @description TODO
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private ProductFeignClient productFeignClient;

    @SentinelResource(value = "createOrder", blockHandler = "createOrderFallback")
    @Override
    public Order createOrder(Long productId, Long userId) {
//        Product product = findProduct(productId);
        Product product = productFeignClient.getProductById(productId);
        Order order = new Order();
        order.setId(1L);
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setUserId(userId);
        order.setUserName("zhangsan");
        order.setAddress("buzhidao");
        order.setProductList(Arrays.asList(product));


        return order;
    }

    private Order createOrderFallback(Long productId, Long userId, BlockException e) {

        Order order = new Order();
        order.setId(0L);
        order.setTotalAmount(new BigDecimal("0"));
        order.setUserId(0L);
        order.setUserName("");
        order.setAddress("异常处理：" + e.getClass());
        return order;
    }

    //普通凡是
    private Product findProduct(Long productId) {
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance serviceInstance = instances.get(0);
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/getProduct/" + productId;

        log.info("order远程调用：{}", url);
        Product product = restTemplate.getForObject(url, Product.class);
        return product;

    }

    //负载模式
    private Product findProductLoad(Long productId) {

        ServiceInstance choose = loadBalancerClient.choose("service-product");
        String url = "http://" + choose.getHost() + ":" + choose.getPort() + "/getProduct/" + productId;

        log.info("order远程调用：{}", url);
        Product product = restTemplate.getForObject(url, Product.class);
        return product;

    }

    //负责模式注解模式
    private Product findProductLoadZhu(Long productId) {

        String url = "http://service-product/getProduct/" + productId;

        log.info("order远程调用：{}", url);
        Product product = restTemplate.getForObject(url, Product.class);
        return product;

    }

}
